﻿<template>
  <a-drawer
    :title="title"
    width="90%"
    :visible="visible"
    @close="
      () => {
        this.visible = false
      }
    "
  >
    <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="客户名称" prop="cCusName">
            <a-input disabled v-model="entity.CusName" autocomplete="off" placeholder="客户名称" />
          </a-form-model-item>
        </a-col>

        <!-- <a-col :span="8">
          <a-form-model-item label="客户编码" prop="cCusCode">
            <a-input disabled v-model="entity.cCusCode" autocomplete="off" placeholder="客户编码" />
          </a-form-model-item>
        </a-col> -->

        <a-col :span="8">
          <a-form-model-item label="发票号码" prop="Invoicenumber">
            <a-input disabled v-model="entity.Invoicenumber" autocomplete="off" placeholder="销售发票号" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
            <a-form-model-item label="单据日期" prop="CreateTime">
              <a-date-picker disabled format="YYYY-MM-DD" style="width: 100%" v-model="entity.CreateTime" />
              <!-- <a-input v-model="entity.dDate" autocomplete="off" placeholder="单据日期" /> -->
            </a-form-model-item>
          </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-model-item label="发票代码" prop="Invoicecode">
            <a-input disabled v-model="entity.Invoicecode" autocomplete="off" placeholder="发票代码" />
          </a-form-model-item>
        </a-col>

        <a-col :span="8">
          <a-form-model-item label="购货方" prop="TicketbuyerName">
            <a-input disabled v-model="entity.TicketbuyerName" autocomplete="off" placeholder="购货方" />
          </a-form-model-item>
        </a-col>
        <a-col :span="8">
            <a-form-model-item label="开户银行" prop="Accbank">
              <a-input disabled v-model="entity.Accbank" autocomplete="off" placeholder="开户银行" />
            </a-form-model-item>
          </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
              <a-form-model-item label="税号" prop="TaxRegcode">
                <a-input disabled v-model="entity.TaxRegcode" autocomplete="off" placeholder="税号" />
              </a-form-model-item>
            </a-col>
        <a-col :span="8">
          <a-form-model-item label="收获地址" prop="PayeeUserAddres">
            <a-textarea disabled v-model="entity.PayeeUserAddres" autocomplete="off" />
            <!-- <a-input v-model="entity.cShipAddress" autocomplete="off" placeholder="发货地址" /> -->
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
    <a-table
      ref="table"
      :scroll="{ y: 530 }"
      :columns="columns"
      :rowKey="(row) => row.Id"
      :dataSource="data"
      :pagination="pagination"
      :loading="loading"
      @change="handleTableChange"
      :bordered="true"
      size="small"
    >
    </a-table>
    <div
      :style="{
        position: 'absolute',
        right: 0,
        bottom: 0,
        width: '100%',
        borderTop: '1px solid #e9e9e9',
        padding: '4px 16px',
        background: '#fff',
        textAlign: 'right',
        zIndex: 1,
      }"
    >
      <a-button @click="close">取消</a-button>&nbsp;
    </div>
  </a-drawer>
</template>

<script>
import moment from 'moment'
const toThousands = function (data) {
  if (data != null) {
    return data.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
  } else {
  }
}
const columns = [
  // { title: '销售发票主表标识', dataIndex: 'SBVID', width: '10%' },
  // { title: '销售发票子表标识', dataIndex: 'AutoID', width: '10%' },
  // { title: '仓库编码', dataIndex: 'cWhCode', width: '10%' },
  // { title: '项目名称', dataIndex: 'cItemName' },
  // { title: '客户存货名称', dataIndex: 'cCusInvName' },
  { title: '产品编码', dataIndex: 'InventoryCode' },
  { title: '产品名称', dataIndex: 'InventoryName' },
  {
    title: '数量',
    dataIndex: 'InvoicingQuantity',
    align: 'right',
    customRender: (value, row, index) => {
      function numberWithCommas(x) {
        x = x.toString()
        var pattern = /(-?\d+)(\d{3})/
        while (pattern.test(x)) x = x.replace(pattern, '$1,$2')
        return x
      }

      return numberWithCommas(value)
    },
  },
  {
    title: '销售单价(元)',
    dataIndex: 'InvoicingPrice',
    align: 'right',
    customRender: (value) => {
      if (value) {
        return Number(value).toFixed(8).replace(/\d(?=(\d{3})+\.)/g, '$&,')
      }
    },
  },

  {
    title: '销售金额(元)',
    dataIndex: 'InvoicingAmount',
    align: 'right',
    customRender: (value) => {
      if (value) {
        return Number(value)
          .toFixed(2)
          .replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,')
      }
    },
  },

  // { title: '原币税额(元)', dataIndex: 'iTax', align: 'right', customRender: toThousands },
  // { title: '辅计量数量', dataIndex: 'iNum' },
  // { title: '单价(元)', dataIndex: 'iQuotedPrice', align: 'right' }
  // { title: '原币无税单价', dataIndex: 'iUnitPrice', width: '10%' },
  // { title: '原币含税单价', dataIndex: 'iTaxUnitPrice', width: '10%' },
  // { title: '原币无税金额', dataIndex: 'iMoney', width: '10%' },
  // { title: '原币价税合计', dataIndex: 'iSum', width: '10%' },
  // { title: '原币折扣额', dataIndex: 'iDisCount', width: '10%' },
  // { title: '本币无税单价', dataIndex: 'iNatUnitPrice', width: '10%' },
  // { title: '本币无税金额', dataIndex: 'iNatMoney', width: '10%' },
  // { title: '本币税额', dataIndex: 'iNatTax', width: '10%' },
  // { title: '本币价税合计', dataIndex: 'iNatSum', width: '10%' },
  // { title: '本币折扣额', dataIndex: 'iNatDisCount', width: '10%' },
  // { title: '原销售发票主表标识', dataIndex: 'iSBVID', width: '10%' },
  // { title: '累计本币收款金额', dataIndex: 'iMoneySum', width: '10%' },
  // { title: '累计原币收款金额', dataIndex: 'iExchSum', width: '10%' },
  // { title: '凭证线索号', dataIndex: 'cClue', width: '10%' },
  // { title: '销售收入科目编码', dataIndex: 'cIncomeSub', width: '10%' },
  // { title: '销售税金科目编码', dataIndex: 'cTaxSub', width: '10%' },
  // { title: '凭证日期', dataIndex: 'dSignDate', width: '10%' },
  // { title: '备注', dataIndex: 'cMemo', width: '10%' },
  // { title: '批次', dataIndex: 'iBatch', width: '10%' },
  // { title: '批号', dataIndex: 'cBatch', width: '10%' },
  // { title: '结算标志', dataIndex: 'bSettleAll', width: '10%' },
  // { title: '存货自由项1', dataIndex: 'cFree1', width: '10%' },
  // { title: '存货自由项2', dataIndex: 'cFree2', width: '10%' },
  // { title: '收发记录子表标识', dataIndex: 'RdsID', width: '10%' },
  // { title: '退补标志', dataIndex: 'iTB', width: '10%' },
  // { title: '失效日期', dataIndex: 'dvDate', width: '10%' },
  // { title: '退补数量', dataIndex: 'TBQuantity', width: '10%' },
  // { title: '退补辅计量数量', dataIndex: 'TBNum', width: '10%' },
  // { title: '销售订单子表标识', dataIndex: 'iSOsID', width: '10%' },
  // { title: '发货退货单子表标识', dataIndex: 'iDLsID', width: '10%' },
  // { title: '扣率', dataIndex: 'KL', width: '10%' },
  // { title: '二次扣率', dataIndex: 'KL2', width: '10%' },
  // { title: '存货名称', dataIndex: 'cInvName', width: '10%' },
  // { title: '税率', dataIndex: 'iTaxRate', width: '10%' },
  // { title: '表体自定义项22', dataIndex: 'cDefine22', width: '10%' },
  // { title: '表体自定义项23', dataIndex: 'cDefine23', width: '10%' },
  // { title: '表体自定义项24', dataIndex: 'cDefine24', width: '10%' },
  // { title: '表体自定义项25', dataIndex: 'cDefine25', width: '10%' },
  // { title: '表体自定义项26', dataIndex: 'cDefine26', width: '10%' },
  // { title: '表体自定义项27', dataIndex: 'cDefine27', width: '10%' },
  // { title: '累计出库数量', dataIndex: 'fOutQuantity', width: '10%' },
  // { title: '累计出库辅计量数量', dataIndex: 'fOutNum', width: '10%' },
  // { title: '项目编码', dataIndex: 'cItemCode', width: '10%' },
  // { title: '项目大类编码', dataIndex: 'cItem_class', width: '10%' },
  // { title: '项目大类名称', dataIndex: 'cItem_CName', width: '10%' },
  // { title: '存货自由项3', dataIndex: 'cFree3', width: '10%' },
  // { title: '存货自由项4', dataIndex: 'cFree4', width: '10%' },
  // { title: '存货自由项5', dataIndex: 'cFree5', width: '10%' },
  // { title: '存货自由项6', dataIndex: 'cFree6', width: '10%' },
  // { title: '存货自由项7', dataIndex: 'cFree7', width: '10%' },
  // { title: '存货自由项8', dataIndex: 'cFree8', width: '10%' },
  // { title: '存货自由项9', dataIndex: 'cFree9', width: '10%' },
  // { title: '存货自由项10', dataIndex: 'cFree10', width: '10%' },
  // { title: '换算率', dataIndex: 'iInvExchRate', width: '10%' },
  // { title: '计量单位编码', dataIndex: 'cUnitID', width: '10%' },
  // { title: '表体自定义项28', dataIndex: 'cDefine28', width: '10%' },
  // { title: '表体自定义项29', dataIndex: 'cDefine29', width: '10%' },
  // { title: '表体自定义项30', dataIndex: 'cDefine30', width: '10%' },
  // { title: '表体自定义项31', dataIndex: 'cDefine31', width: '10%' },
  // { title: '表体自定义项32', dataIndex: 'cDefine32', width: '10%' },
  // { title: '表体自定义项33', dataIndex: 'cDefine33', width: '10%' },
  // { title: '表体自定义项34', dataIndex: 'cDefine34', width: '10%' },
  // { title: '表体自定义项35', dataIndex: 'cDefine35', width: '10%' },
  // { title: '表体自定义项36', dataIndex: 'cDefine36', width: '10%' },
  // { title: '表体自定义项37', dataIndex: 'cDefine37', width: '10%' },
  // { title: '生产日期', dataIndex: 'dMDate', width: '10%' },
  // { title: '采购发票子表标识', dataIndex: 'iPBVsID', width: '10%' },
  // { title: '入库单据号', dataIndex: 'cCode', width: '10%' },
  // { title: '销售订单号', dataIndex: 'cSoCode', width: '10%' },
  // { title: '质检标记', dataIndex: 'bgsp', width: '10%' },
  // { title: 'PTO母件顺序号', dataIndex: 'iPPartSeqID', width: '10%' },
  // { title: '母件物料ID', dataIndex: 'iPPartID', width: '10%' },
  // { title: '母件数量', dataIndex: 'iPPartQty', width: '10%' },
  // { title: '合同号', dataIndex: 'cContractID', width: '10%' },
  // { title: '合同标的', dataIndex: 'cContractTagCode', width: '10%' },
  // { title: '合同cGuid', dataIndex: 'cContractRowGuid', width: '10%' },
  // { title: '保质期', dataIndex: 'iMassDate', width: '10%' },
  // { title: '保质期单位', dataIndex: 'cMassUnit', width: '10%' },
  // { title: '是否质检', dataIndex: 'bQANeedCheck', width: '10%' },
  // { title: '是否急料', dataIndex: 'bQAUrgency', width: '10%' },
  // { title: '客户存货编码', dataIndex: 'cCusInvCode', width: '10%' },
  // { title: '记账人', dataIndex: 'cbaccounter', width: '10%' },
  // { title: '是否记账', dataIndex: 'bcosting', width: '10%' },
  // { title: '订单号', dataIndex: 'cordercode', width: '10%' }
  // { title: '订单行号', dataIndex: 'iorderrowno', width: '10%' },
  // { title: '客户最低售价', dataIndex: 'fcusminprice', width: '10%' },
  // { title: '供应商编码', dataIndex: 'cvmivencode', width: '10%' },
  // { title: '供应商名称', dataIndex: 'cVenAbbName', width: '10%' },
  // { title: '行号', dataIndex: 'irowno', width: '10%' },
  // { title: '有效期推算方式', dataIndex: 'iExpiratDateCalcu', width: '10%' },
  // { title: '有效期计算项', dataIndex: 'dExpirationdate', width: '10%' },
  // { title: '有效期至', dataIndex: 'cExpirationdate', width: '10%' },
  // { title: '批次属性1', dataIndex: 'cbatchproperty1', width: '10%' },
  // { title: '批次属性2', dataIndex: 'cbatchproperty2', width: '10%' },
  // { title: '批次属性3', dataIndex: 'cbatchproperty3', width: '10%' },
  // { title: '批次属性4', dataIndex: 'cbatchproperty4', width: '10%' },
  // { title: '批次属性5', dataIndex: 'cbatchproperty5', width: '10%' },
  // { title: '批次属性6', dataIndex: 'cbatchproperty6', width: '10%' },
  // { title: '批次属性7', dataIndex: 'cbatchproperty7', width: '10%' },
  // { title: '批次属性8', dataIndex: 'cbatchproperty8', width: '10%' },
  // { title: '批次属性9', dataIndex: 'cbatchproperty9', width: '10%' },
  // { title: '批次属性10', dataIndex: 'cbatchproperty10', width: '10%' },
  // {
  //   title: '需求跟踪方式（1-销售订单行号；4-需求分类号；5-销售订单号）',
  //   dataIndex: 'idemandtype',
  //   align: 'center',
  //   width: '10%'
  // },
  // { title: '需求分类号', dataIndex: 'cdemandcode', width: '10%' },
  // { title: '需求分类说明', dataIndex: 'cdemandmemo', width: '10%' },
  // { title: '需求跟踪id', dataIndex: 'cdemandid', width: '10%' },
  // { title: '需求跟踪行号', dataIndex: 'idemandseq', width: '10%' },
  // { title: '发货单号', dataIndex: 'cbdlcode', width: '10%' },
  // { title: '序列号个数', dataIndex: 'iInvSNCount', width: '10%' },
  // { title: '序列号', dataIndex: 'cInvSN', width: '10%' },
  // { title: '退货原因编码', dataIndex: 'cReasonCode', width: '10%' },
  // { title: '入库单供应商编码', dataIndex: 'cvencode', width: '10%' },
  // { title: '需要签回', dataIndex: 'bneedsign', width: '10%' },
  // { title: '收款单号', dataIndex: 'cgathingcode', width: '10%' },
  // { title: '合同质保金原币含税金额', dataIndex: 'ftaxpasum', width: '10%' },
  // { title: '合同质保金原币无税金额', dataIndex: 'fpasum', width: '10%' },
  // { title: '合同质保金本币含税金额', dataIndex: 'fnattaxpasum', width: '10%' },
  // { title: '合同质保金本币无税金额', dataIndex: 'fnatpasum', width: '10%' },
  // { title: 'body_outid', dataIndex: 'body_outid', width: '10%' },
  // { title: '无', dataIndex: 'cPZNum', width: '10%' },
  // { title: '入库单单据类型', dataIndex: 'cInVouchType', width: '10%' },
  // { title: '预定仓库编码', dataIndex: 'cBookWhcode', width: '10%' },
  // { title: '成本数量', dataIndex: 'icostquantity', width: '10%' },
  // { title: '成本金额', dataIndex: 'icostsum', width: '10%' },
  // { title: '货位', dataIndex: 'cPosition', width: '10%' },
  // { title: '记帐日期', dataIndex: 'dkeepdate', width: '10%' },
  // { title: '出库单子表id', dataIndex: 'isaleoutid', width: '10%' },
  // { title: '报价含税标识', dataIndex: 'bsaleprice', width: '10%' },
  // { title: '是否赠品', dataIndex: 'bgift', width: '10%' },
  // { title: '出库标识', dataIndex: 'cbsaleout', width: '10%' },
  // { title: '订单关闭标识', dataIndex: 'bmpforderclosed', width: '10%' },
  // { title: '单据行条码', dataIndex: 'cbSysBarCode', width: '10%' },
  // { title: '权重比例', dataIndex: 'fchildrate', width: '10%' },
  // { title: '父节点编码', dataIndex: 'cParentCode', width: '10%' },
  // { title: '子节点编码', dataIndex: 'cChildCode', width: '10%' },
  // { title: '使用数量', dataIndex: 'fchildqty', width: '10%' },
  // { title: '发货模式', dataIndex: 'iCalcType', width: '10%' },
  // { title: 'fretailrealamount', dataIndex: 'fretailrealamount', width: '10%' },
  // { title: 'fretailapportionamount', dataIndex: 'fretailapportionamount', width: '10%' },
  // { title: 'fretailsettleamount', dataIndex: 'fretailsettleamount', width: '10%' },
  // {
  //   title: '创建时间',
  //   dataIndex: 'CreateTime',
  //   width: '10%',
  //   align: 'center',
  //   scopedSlots: { customRender: 'CreateTime' }
  // },
  // { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  props: {
    parentObj: Object,
  },
  data() {
    return {
      columns,
      data: [],
      selectedRowKeys: [],
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true,
      },
      filters: {},
      sorter: { field: 'SBVID', order: 'desc' },
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 },
      },
      visible: false,
      loading: false,
      entity: {},
      rules: {},
      title: '',
    }
  },
  methods: {
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }
      this.getDataList()
    },
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    init() {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
    },
    close() {
      this.visible = false

      this.entity = {}
    },
    openForm(id, title) {
      this.init()
      this.title = title
      //console.log(id)
      if (id) {
        this.loading = true
        this.$http.post('/InvoiceManagement/Inv_Invoicing/GetTheData', { id: id }).then((resJson) => {
          this.loading = false
          this.entity = resJson.Data
          if (this.entity['CreateTime']) {
            this.entity['CreateTime'] = moment(this.entity['CreateTime'])
          }
        })
        this.$http
          .post('/InvoiceManagement/v_invoicesdetail/GetDataList', {
            PageIndex: this.pagination.current,
            PageRows: this.pagination.pageSize,
            SortField: 'Id',
            SortType: this.sorter.order, DeptId: this.DeptId,
            Search: { ParentId: id },
            ...this.filters,
          })
          .then((resJson) => {
            this.loading = false
            this.data = resJson.Data
            if (this.data) {
              const pagination = { ...this.pagination }
              pagination.total = resJson.Total
              this.pagination = pagination
            }
          })
      }
    },
    handleSubmit() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return
        }
        this.loading = true
        this.$http.post('/OrderManagement/SaleBillVouch/SaveData', this.entity).then((resJson) => {
          this.loading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.visible = false

            this.parentObj.getDataList()
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    },
  },
}
</script>
